package com.google.ma.rssreader;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class NewsItemAdapter extends BaseAdapter {

    private LayoutInflater inflater;

    private int layout;

    private ArrayList<HashMap<String, String>> newsList;

    private String[] keys;

    private int[] views;

    public NewsItemAdapter(Context context, ArrayList<HashMap<String, String>> newsList, int layout, String[] keys, int[] views) {
        this.inflater = LayoutInflater.from(context);
        this.layout = layout;
        this.newsList = newsList;
        this.keys = keys;
        this.views = views;
    }

    @Override
    public int getCount() {
        return newsList.size();
    }

    @Override
    public Object getItem(int position) {
        return newsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(layout, null);
            holder = new ViewHolder();
            holder.view0 = (TextView) convertView.findViewById(views[0]);
            holder.view1 = (TextView) convertView.findViewById(views[1]);
            holder.view2 = (TextView) convertView.findViewById(views[2]);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String data0 = (String) newsList.get(position).get(keys[0]);
        String data1 = (String) newsList.get(position).get(keys[1]);
        String data2 = (String) newsList.get(position).get(keys[2]);
        holder.view0.setText(data0);
        holder.view1.setText(data1);
        holder.view2.setText(data2);
        return convertView;
    }

    private class ViewHolder {
        TextView view0;
        TextView view1;
        TextView view2;
    }
}
